{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "import numpy\n",
    "import math\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "matplotlib.style.use('ggplot')\n",
    "matplotlib.rcParams.update({'font.size': 12})\n",
    "matplotlib.rcParams.update({'xtick.labelsize': 'x-large'})\n",
    "matplotlib.rcParams.update({'xtick.major.size': '0'})\n",
    "matplotlib.rcParams.update({'ytick.labelsize': 'x-large'})\n",
    "matplotlib.rcParams.update({'ytick.major.size': '0'})\n",
    "matplotlib.rcParams.update({\"text.usetex\": True})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "facecolor='C4'\n",
    "edgecolor='C3'\n",
    "vertexcolor='C0'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# convex polygon\n",
    "x = [-0.4, 0.5, 0.4, 0, -0.5]\n",
    "y = [-0.4, -0.5, 0.1, 0.5, 0.5]\n",
    "\n",
    "fig = matplotlib.figure.Figure(figsize=(7, 4.32624056), dpi=100)\n",
    "ax = fig.add_subplot(aspect='equal')\n",
    "ax.fill(x, y, facecolor=facecolor, edgecolor=edgecolor, linewidth=3)\n",
    "\n",
    "for i,v in enumerate(zip(x,y)):\n",
    "    vx,vy = v[0] + 0.01, v[1] + 0.02\n",
    "    ax.text(vx,vy, r'$\\vec{v}_' + f'{i}' + '$', fontsize=20)\n",
    "    ax.plot(v[0], v[1], '.', color=vertexcolor, markersize=15)\n",
    "\n",
    "ax.plot(0, 0, '.', color=vertexcolor, markersize=15)\n",
    "ax.text(0.01, 0.02, r'$\\vec{0}$', fontsize=20)\n",
    "\n",
    "ax.set_xlabel('X')\n",
    "ax.set_ylabel('Y')\n",
    "\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fig.savefig('../convex-polygon.svg', bbox_inches='tight', facecolor=(1, 1, 1, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# simple polygon\n",
    "x = [-0.4, 0.5, 0.4, 0, -0.5]\n",
    "y = [-0.4, -0.5, 0.4, 0.2, 0.5]\n",
    "\n",
    "fig = matplotlib.figure.Figure(figsize=(7, 4.32624056), dpi=100)\n",
    "ax = fig.add_subplot(aspect='equal')\n",
    "ax.fill(x, y, facecolor=facecolor, edgecolor=edgecolor, linewidth=3)\n",
    "\n",
    "for i,v in enumerate(zip(x,y)):\n",
    "    vx,vy = v[0] + 0.01, v[1] + 0.02\n",
    "    ax.text(vx,vy, r'$\\vec{v}_' + f'{i}' + '$', fontsize=20)\n",
    "    ax.plot(v[0], v[1], '.', color=vertexcolor, markersize=15)\n",
    "\n",
    "ax.plot(0, 0, '.', color=vertexcolor, markersize=15)\n",
    "ax.text(0.01, 0.02, r'$\\vec{0}$', fontsize=20)\n",
    "\n",
    "ax.set_xlabel('X')\n",
    "ax.set_ylabel('Y')\n",
    "\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig.savefig('../simple-polygon.svg', bbox_inches='tight', facecolor=(1, 1, 1, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# convex spheropolygon\n",
    "x_vert = [-0.5, 0.5, 0.5, -0.5]\n",
    "y_vert = [-0.5, -0.5, 0.5, 0.5]\n",
    "sweep_radius = 0.3\n",
    "\n",
    "n_theta = 20\n",
    "\n",
    "x = []\n",
    "y = []\n",
    "\n",
    "# hard coded for a rectangle\n",
    "for i,v in enumerate(zip(x_vert,y_vert)):\n",
    "    if i & 1 == 0:\n",
    "        x_start = v[0] + numpy.sign(v[0])*sweep_radius  \n",
    "        y_start = v[1]\n",
    "\n",
    "        x_end = v[0]      \n",
    "        y_end = v[1] + numpy.sign(v[1])*sweep_radius\n",
    "    else:\n",
    "        x_end = v[0] + numpy.sign(v[0])*sweep_radius  \n",
    "        y_end = v[1]\n",
    "\n",
    "        x_start = v[0]      \n",
    "        y_start = v[1] + numpy.sign(v[1])*sweep_radius\n",
    "        \n",
    "    theta_start = math.atan2(y_start - v[1], x_start - v[0])\n",
    "    theta_end = math.atan2(y_end - v[1], x_end - v[0])\n",
    "    \n",
    "    if theta_start > theta_end:\n",
    "        theta_end += 2 * math.pi\n",
    "    \n",
    "    theta = numpy.linspace(theta_start, theta_end, num=n_theta)\n",
    "    for t in theta:\n",
    "        x.append(v[0] + sweep_radius * math.cos(t))\n",
    "        y.append(v[1] + sweep_radius * math.sin(t))\n",
    "        \n",
    "fig = matplotlib.figure.Figure(figsize=(7, 4.32624056), dpi=100)\n",
    "ax = fig.add_subplot(aspect='equal')\n",
    "ax.fill(x, y, facecolor=facecolor, edgecolor=edgecolor, linewidth=3)\n",
    "\n",
    "ax.add_patch(matplotlib.patches.Circle((x_vert[0], y_vert[0]), sweep_radius, color='C1'))\n",
    "ax.plot([x_vert[0], x_vert[0] - sweep_radius * math.cos(math.pi/4)],\n",
    "        [y_vert[0], y_vert[0] - sweep_radius * math.sin(math.pi/4)],\n",
    "        color='C5',\n",
    "        linewidth=3)\n",
    "        \n",
    "ax.text(x_vert[0] - 0.5 * sweep_radius * math.cos(math.pi/4), \n",
    "        y_vert[0] - 0.5 * sweep_radius * math.sin(math.pi/4) - 0.06, \n",
    "        '$s_r$', \n",
    "        fontsize=20)\n",
    "    \n",
    "for i,v in enumerate(zip(x_vert,y_vert)):\n",
    "    vx,vy = v[0] + 0.01, v[1] + 0.02\n",
    "    ax.text(vx,vy, r'$\\vec{v}_' + f'{i}' + '$', fontsize=20)\n",
    "    ax.plot(v[0], v[1], '.', color=vertexcolor, markersize=15)\n",
    "\n",
    "ax.plot(0, 0, '.', color=vertexcolor, markersize=15)\n",
    "ax.text(0.01, 0.02, r'$\\vec{0}$', fontsize=20)\n",
    "\n",
    "ax.set_xlabel('X')\n",
    "ax.set_ylabel('Y')\n",
    "\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig.savefig('../convex-spheropolygon.svg', bbox_inches='tight', facecolor=(1, 1, 1, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Convex polyhedron\n",
    "verts = numpy.zeros(shape=(8,3))\n",
    "verts[0,:] = [-10,-10,-10]\n",
    "verts[1,:] = [10,-10,-10]\n",
    "verts[2,:] = [5, 5, -5]\n",
    "verts[3,:] = [-10, 10, -10]\n",
    "verts[4,:] = [-10, -10, 10]\n",
    "verts[5,:] = [10,-10, 10]\n",
    "verts[6,:] = [5, 5, 5]\n",
    "verts[7,:] = [-10, 10, 10]\n",
    "\n",
    "lines = [[0, 1], [1, 2], [2, 3], [3, 0], [4, 5], [5, 6], [6, 7], [7, 4], [0, 4], [1, 5], [2, 6], [3, 7]]\n",
    "\n",
    "fig = matplotlib.figure.Figure(figsize=(7, 4.32624056), dpi=100)\n",
    "ax = fig.add_subplot(projection='3d', proj_type='ortho')\n",
    "for line in lines:\n",
    "    ax.plot([verts[line[0],0], verts[line[1],0]],\n",
    "            [verts[line[0],1], verts[line[1],1]],\n",
    "            [verts[line[0],2], verts[line[1],2]], color=edgecolor)\n",
    "\n",
    "for i,v in enumerate(verts):\n",
    "    vx,vy,vz = v[0], v[1] + 2.5, v[2]\n",
    "    ax.text(vx,vy,vz, r'$\\vec{v}_' + f'{i}' + '$', fontsize=20)\n",
    "    ax.plot(v[0], v[1], v[2], '.', color=vertexcolor, markersize=15)\n",
    "\n",
    "ax.plot(0, 0, 0, '.', color=vertexcolor, markersize=15)    \n",
    "ax.text(0, -3.5, 0, r'$\\vec{0}$', fontsize=20)\n",
    "\n",
    "ax.set_xlabel('X')\n",
    "ax.set_ylabel('Y')\n",
    "ax.set_zlabel('Z')\n",
    "ax.view_init(azim=10, elev=20, vertical_axis='y')\n",
    "ax.set_facecolor('white')\n",
    "\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig.savefig('../convex-polyhedron.svg', bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
